package com.bs.bszhsq.controller;

import cn.hutool.core.bean.BeanUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.entity.Fix;
import com.bs.bszhsq.entity.Users;
import com.bs.bszhsq.exception.ServiceException;
import com.bs.bszhsq.service.IUsersService;
import com.bs.bszhsq.utils.TokenUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

import com.bs.bszhsq.service.IForumService;
import com.bs.bszhsq.entity.Forum;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * <p>
 * 社区信息 前端控制器
 * </p>
 *
 * @author 章宇光
 * @since 2024-03-31
 */
@RestController
@RequestMapping("/forum")
@Api(tags = "社区信息接口")
        public class ForumController {
    //service注入
@Resource
private IForumService forumService;
@Resource
private IUsersService usersService;


@PostMapping
@ApiOperation(value = "新增")
public R save(@RequestBody Forum forum){
        if (BeanUtil.isEmpty(forum)){
                throw new ServiceException("500","传入参数不完整！");
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        String userId = JWT.decode(token).getAudience().get(0);
        Users user= usersService.getById(userId);
        String username = user.getUsername();
        //姓名
        forum.setName(username);
        forum.setAddtime(LocalDate.now());
        //赞
        forum.setPraise(0);
        //踩
        forum.setTread(0);
        boolean save= forumService.save(forum);
        return R.write(save);
        }


@PostMapping("/delete")
@ApiOperation(value = "删除")
public R delete(@RequestBody Long id){
        boolean b=forumService.removeById(id);
        return R.write(b);
        }

@PostMapping("/update")
@ApiOperation(value = "修改")
public R update(@RequestBody Forum forum) {
        boolean update = forumService.updateById(forum);
        return R.write(update);
        }


@PostMapping("/deletebatch")
@ApiOperation(value = "批量删除")
public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = forumService.removeByIds(ids);
        return R.write(b);
        }


@GetMapping("/page")
@ApiOperation(value = "分页查询")
public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam(defaultValue = "") String name) {
        IPage<Forum> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Forum> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
                qw.like("name", name);
        }
        qw.orderByDesc("id");
        IPage<Forum> page1 = forumService.page(page, qw);
        return R.success(page1);
        }

@GetMapping("/one")
@ApiOperation(value = "文章详情")
public R getOne(Long id){
        Forum forum = forumService.getById(id);
        return R.success(forum);
}







        }
